const query = require('../db/query')

// 获取收货地址
const getAddress = ()=>{
    let sql = `select * from addres`
    return query(sql)
}

// 新增收货地址
const addAddress = (params) =>{
    if(params.state === '1'){
        let sql = `update addres set addresstate = 0`
        query(sql)
    }
    let sql = `insert into addres (addresname, addresphone, addrescity, addresdesc, addresstate) values ('${params.name}', '${params.phone}', '${params.city}', '${params.desc}', '${params.state}')`
    return query(sql)
}

// 修改收货地址
const updateAddress = (params) =>{
    let sql = `update addres set addresname = '${params.name}', addresphone = '${params.phone}', addrescity = '${params.city}', addresdesc = '${params.desc}', addresstate = '${params.state}' where id = ${params.id}`
    return query(sql)
}

// 删除收货地址
const deleteAddress = (params) => {
    let sql = `delete from addres where id = '${params.id}'`
    return query(sql)
}

// 设置默认地址
const setDefaultAddress = (params) => {
    let sql = `update addres set addresstate = 0`
    query(sql)
    let sql1 = `update addres set addresstate = 1 where id = ${params.id}`
    return query(sql1)
}

module.exports = {
    getAddress,
    addAddress,
    updateAddress,
    deleteAddress,
    setDefaultAddress
}